﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace HKTools
{
    public class HK_CollapsibleMenuCtrl : HK_CollapsibleMenuController
    {
        [SerializeField] ScrollRect scrollRect;
        [SerializeField] Scrollbar scrollBar;

        float scrollRectHeight;

        List<GameObject> itemPanelList;

        [SerializeField] GameObject menuItem_L1;
        [SerializeField] GameObject menuItem_L2;
        [SerializeField] GameObject menuItem_L3;

        [SerializeField] Transform content;

        void Start()
        {
            itemPanelList = new List<GameObject>();

            scrollRectHeight = scrollRect.GetComponent<RectTransform>().rect.height;

            HK_MenuItem L1_1 = AddItem_L1("基础数据");

            AddItem_L2("飞机介绍", L1_1);
            AddItem_L2("结构强度", L1_1);
            AddItem_L2("重量分布", L1_1);
            AddItem_L2("飞机结构拆解", L1_1);

            HK_MenuItem L1_2 = AddItem_L1("基础图册");

            AddItem_L2("机背高清图", L1_2);
            AddItem_L2("机腹高清图", L1_2);
            AddItem_L2("飞机三视图", L1_2);
            AddItem_L2("舱段划分图", L1_2);
            AddItem_L2("驾驶舱图", L1_2);

            HK_MenuItem L1_3 = AddItem_L1("涉密设备");

            HK_MenuItem L2_3_1 = AddItem_L2("黑匣子", L1_3);
            AddItem_L3("位置", L2_3_1);
            AddItem_L3("连接方式", L2_3_1);
            AddItem_L3("重量体积", L2_3_1);
            AddItem_L3("拆卸方法", L2_3_1);

            AddItem_L2("漂浮式记录器", L1_3);
            AddItem_L2("快速记录器", L1_3);
            AddItem_L2("敌我识别设备", L1_3);
            AddItem_L2("保密机", L1_3);

            HK_MenuItem L1_4 = AddItem_L1("危爆物品");

            AddItem_L2("油箱", L1_4);
            AddItem_L2("干扰弹", L1_4);
            AddItem_L2("火箭弹", L1_4);
            AddItem_L2("炸弹", L1_4);
            AddItem_L2("航炮炮弹", L1_4);
            AddItem_L2("导弹", L1_4);
            AddItem_L2("弹射火箭弹", L1_4);
            AddItem_L2("爆炸螺栓", L1_4);

            HK_MenuItem L1_5 = AddItem_L1("救生装备");

            AddItem_L2("座椅", L1_5);
            AddItem_L2("头盔", L1_5);
            AddItem_L2("氧气系统", L1_5);
            AddItem_L2("服装", L1_5);
            AddItem_L2("救生物品", L1_5);

            HK_MenuItem L1_6 = AddItem_L1("示位报警装置");

            scrollBar.value = 0;

            this.RegisterEvent<Event_MenuButtonClick>(args =>
            {
                ChangeContentPadding();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            ChangeContentPadding();
        }

        void ChangeContentPadding()
        {
            ActionKit.Sequence()
                .Callback(() => content.GetComponent<VerticalLayoutGroup>().padding.bottom = 0)
                .DelayFrame(1)
                .Callback(() =>
                {
                    if (content.GetComponent<RectTransform>().rect.height < scrollRectHeight)
                    {
                        content.GetComponent<VerticalLayoutGroup>().padding.bottom = (int)(scrollRectHeight - content.GetComponent<RectTransform>().rect.height);
                    }
                }).Start(this);
        }

        HK_MenuItem AddItem_L1(string itemName)
        {
            GameObject newItemPanel = Instantiate(menuItem_L1);
            itemPanelList.Add(newItemPanel);
            HK_MenuItem menuItem = newItemPanel.GetComponent<HK_MenuItem>();
            menuItem.SetParent(content);
            menuItem.InitPanelContent(new HK_MenuData(itemName, 1));
            return menuItem;
        }

        HK_MenuItem AddItem_L2(string itemName, HK_MenuItem parent)
        {
            GameObject newItemPanel = Instantiate(menuItem_L2);
            itemPanelList.Add(newItemPanel);
            HK_MenuItem menuItem = newItemPanel.GetComponent<HK_MenuItem>();
            menuItem.SetParent(parent);
            menuItem.InitPanelContent(new HK_MenuData(itemName, 2));
            return menuItem;
        }

        void AddItem_L3(string itemName, HK_MenuItem parent)
        {
            GameObject newItemPanel = Instantiate(menuItem_L3);
            itemPanelList.Add(newItemPanel);
            HK_MenuItem menuItem = newItemPanel.GetComponent<HK_MenuItem>();
            menuItem.SetParent(parent);
            menuItem.InitPanelContent(new HK_MenuData(itemName, 3));
        }
    }
}